package com.smartphonedev.overlay;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> 
{
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private Handler addOnMapHandler;
	public int onTapLong, onTapLat;
	MyCount counter;
	public Handler getAddOnMapHandler() {
		return addOnMapHandler;
	}

	public void setAddOnMapHandler(Handler addOnMapHandler) {
		this.addOnMapHandler = addOnMapHandler;
		this.counter = null;
	}

	public MyItemizedOverlay(Drawable defaultMarker, Context context)
	{
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		//		this.addOnMapHandler = addOnMapHandler;
		if(addOnMapHandler != null){
			counter = new MyCount(1000,1000);

		}
	}

	public void addOverlay(OverlayItem overlay)
	{
		mOverlays.add(overlay);
		populate();
	}
	@Override
	protected OverlayItem createItem(int i)
	{
		return mOverlays.get(i);
	}
	@Override
	public int size()
	{
		return mOverlays.size();
	}
	@Override
	protected boolean onTap(int index)
	{
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}
	@Override
	public boolean onTap(GeoPoint point, MapView mapView) 
	{
		return true;
	}

	private long systemTime = -1;
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			systemTime = System.currentTimeMillis();
			if(counter != null){
				counter.start();
				GeoPoint p = mapView.getProjection().fromPixels(
						(int) event.getX(),
						(int) event.getY());

				onTapLat = p.getLatitudeE6();
				onTapLong = p.getLongitudeE6();
			}
			break;
		case MotionEvent.ACTION_UP    :
			if ((System.currentTimeMillis() - systemTime) < 100 && systemTime != Long.MAX_VALUE) {
				mapView.getController().zoomInFixing((int) event.getX(), (int) event.getY());
			}
			if(counter != null){
				counter.cancel();
			}
			systemTime = Long.MAX_VALUE;
			break;
		case MotionEvent.ACTION_MOVE:
			if(counter != null){
				counter.cancel();
			}
			systemTime = Long.MAX_VALUE;
			break;
		}

		return false;
	}
	public class MyCount extends CountDownTimer{
		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			sendMessage();
			counter.cancel();
		}

		@Override
		public void onTick(long millisUntilFinished) {
		}


	}
	private void sendMessage(){
		if(this.addOnMapHandler != null){
			//Constants.vibrator.vibrate(50);
			Message msg = new Message();
			msg.arg1 = onTapLong;
			msg.arg2 = onTapLat;
			this.addOnMapHandler.sendMessage(msg);
		}
	}

}
